
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstdlib>
#include <unistd.h>
#include <strings.h>
#include <string.h>

#include <netinet/in.h>
#include <arpa/inet.h>

//在客户端进行多线程化，一个线程循环收消息，另一个线程可以实现发送消息，这一切都基于UDP的全双工特性
#include <pthread.h>

using namespace std;
struct ThreadData
{
    struct sockaddr_in server;
    int sockfd;
};
// void* recv_message(void* args)
// {
//     //循环接受消息
//     ThreadData* td = static_cast<ThreadData*>(args);
//     char buffer[1024];
//     while(true)
//     {
//         memset(buffer, 0, sizeof(buffer));
//         struct sockaddr_in temp;
//         socklen_t len = sizeof(temp);

//         ssize_t s = recvfrom(td->sockfd, buffer, 1023, 0, (sockaddr*)&temp, &len);
//         if(s > 0)// s > 0收消息成功
//         {
//             buffer[s] = 0;
//             cerr << buffer << endl;
//         }
//     }
// }

// void* send_message(void* args)
// {
//     ThreadData* td = static_cast<ThreadData*>(args);

//     string message;
//     socklen_t len = sizeof(td->server);
//     while(true)
//     {
//         // 发送报文
//         cout << "Please Enter>>";
//         getline(cin, message);

//         sendto(td->sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&(td->server), len);
//     }
// }

void *recv_message(void *args)
{
    // OpenTerminal();
    ThreadData *td = static_cast<ThreadData *>(args);
    char buffer[1024];
    while (true)
    {
        memset(buffer, 0, sizeof(buffer));
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);

        ssize_t s = recvfrom(td->sockfd, buffer, 1023, 0, (struct sockaddr *)&temp, &len);
        if (s > 0)
        {
            buffer[s] = 0;
            cerr << buffer << endl;
        }
    }
}

void *send_message(void *args)
{
    ThreadData *td = static_cast<ThreadData *>(args);
    string message;
    socklen_t len = sizeof(td->server);

    //std::string welcome = td->serverip;
    //welcome += " comming...";
    sendto(td->sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&(td->server), len);

    while (true)
    {
        cout << "Please Enter@ ";
        getline(cin, message);

        // std::cout << message << std::endl;
        // 1. 数据 2. 给谁发
        sendto(td->sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&(td->server), len);
    }
}
void Usage(string proc)
{
    cout << "\n\tUsage:" << proc << "port[1024+ ]\n"
         << endl;
}
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(0);
    }

    string serverip = argv[1];
    uint16_t serverport = stoi(argv[2]);


    ThreadData td;
    //创建服务端的套接字信息
    bzero(&td.server, sizeof(td.server));
    td.server.sin_family = AF_INET;
    td.server.sin_port = htons(serverport);
    td.server.sin_addr.s_addr = inet_addr(serverip.c_str());

    // int socket(int domain, int type, int protocol);
    td.sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    // 使用ipv4协议、面向用户数据报、协议段为0
    if (td.sockfd < 0)
    {
        cout << "socker error" << endl;
        return 1;
    }


    pthread_t recvr, sender;
    pthread_create(&recvr, nullptr, recv_message, &td);
    pthread_create(&sender, nullptr, send_message, &td);

    pthread_join(recvr,nullptr);
    pthread_join(sender,nullptr);

    close(td.sockfd);
    return 0;
}